package com.qf.controller;

import com.qf.common.dto.MemberRequest;
import com.qf.common.exception.ControllerException;
import com.qf.common.vo.MemberVo;
import com.qf.common.vo.ResponseCode;
import com.qf.common.vo.ResponseResult;
import com.qf.config.ShiroUtil;
import com.qf.service.MemberService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author KING
 * @ClassName: com.qf.controller.UserController
 * @Description: 该类的功能描述
 * @date: 2021/10/22 14:02
 */
@RestController
@RequestMapping("/member")
@Validated
public class MemberController {
    @Resource
    private MemberService memberService;

    /**
     * 登录接口
     *
     * @param memberRequest
     * @return
     */
    @PostMapping("/login")
    public ResponseResult<String> login(@Validated(value = MemberRequest.MemberLogin.class)
                                        @RequestBody MemberRequest memberRequest) {

        Subject subject = SecurityUtils.getSubject();
        //将用户名和密码封装成token对象,登录令牌
        UsernamePasswordToken token = new UsernamePasswordToken(memberRequest.getUsername(), memberRequest.getPassword());
        //设置记住我
        token.setRememberMe(true);
        try {
            subject.login(token);
        } catch (UnknownAccountException | IncorrectCredentialsException ex) {
            //账户不存在
            throw new ControllerException(ResponseCode.UNKNOWN_ACCOUNT);
        }
        Session session = SecurityUtils.getSubject().getSession();
        return ResponseResult.success(session.getId().toString());
    }

    /**
     * 返回当前用户信息，不需要传参数,shiro从session中拿
     *
     * @return
     */
    @PostMapping("/info")
    public ResponseResult<MemberVo> getInfo() {
        MemberVo memberVo = ShiroUtil.getMemberVoInfo();
        return ResponseResult.success(memberVo);
    }

    /**
     * 用户注册接口
     * <p>
     * {
   "username": "李四",
  "password": "D123456578"
     * }
     *
     * @param memberRequest 必传参数 nickname、username、password、phone
     * @return
     */
    @PostMapping("/register")
    public ResponseResult<Boolean> register(@Validated(value = {MemberRequest.MemberAdd.class})
                                            @RequestBody MemberRequest memberRequest) {
        Boolean insert = null;
        insert = memberService.register(memberRequest);
        return ResponseResult.success(insert);
    }

    /**
     * 用户修改接口
     *
     * @param memberRequest
     * @return
     */
    @PostMapping("/update")
    public ResponseResult<Boolean> updateMember(@Validated(value = MemberRequest.MemberUpdate.class)
                                                @RequestBody MemberRequest memberRequest) {
        Boolean update = memberService.updateMember(memberRequest);
        return ResponseResult.success(update);
    }

}

